package cn.soqi.img;/**
 * Created by user on 2017/5/8.
 */

/**
 * Zip文件解压工具类
 *
 * @author brozer
 * @email 798121446@qq.com
 * @create 2017-05-08 10:27
 */
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;

import java.io.*;
import java.util.Enumeration;

//使用org.apache.tools.zip这个就不会中文乱码

//使用java.util.zip原生ZipOutputStream与ZipEntry会中文乱码
//import java.util.zip.ZipEntry;
//import java.util.zip.ZipFile;

public class ZipDeCompress
{
    static String zipPath = "C:/Users/user/Desktop/zip.zip";//需要解压的压缩文件
    static String outPath = "C:/Users/user/Desktop/";//解压完成后保存路径,记得"\\"结尾哈
    public static void deCompress(String zipPath,String outPath) throws Exception
    {
        zipPath=zipPath.replace("\\","/");
        outPath=outPath.replace("\\","/");
        if(!outPath.endsWith("/"))
            outPath=outPath+"/";
        ZipFile zipFile = new ZipFile(zipPath,"GBK");//压缩文件的实列,并设置编码
        //获取压缩文中的所以项
        for(Enumeration<ZipEntry> enumeration = zipFile.getEntries();enumeration.hasMoreElements();)
        {
            ZipEntry zipEntry = enumeration.nextElement();//获取元素
            //排除空文件夹
            if(!zipEntry.getName().endsWith(File.separator))
            {
                System.out.println("正在解压文件:"+zipEntry.getName());//打印输出信息
                //创建解压目录
                String zipEntryStr=zipEntry.getName();
                String fileNamePart=zipEntry.getName().substring(zipEntry.getName().lastIndexOf("/")+1,zipEntry.getName().length());
                if(fileNamePart.trim().equals("")){
                    File f = new File(outPath+zipEntry.getName().substring(0, zipEntry.getName().lastIndexOf("/")));
                    //判断是否存在解压目录
                    if(!f.exists())
                    {
                        f.mkdirs();//创建解压目录
                    }
                }else {
                    OutputStream os = new FileOutputStream(outPath + zipEntry.getName());//创建解压后的文件
                    BufferedOutputStream bos = new BufferedOutputStream(os);//带缓的写出流
                    InputStream is = zipFile.getInputStream(zipEntry);//读取元素
                    RgbToCmyk.rgbToCmyk(is,outPath+zipEntry.getName()); //解压文件，同时转换生成对应的CMYK文件
                    BufferedInputStream bis = new BufferedInputStream(is);//读取流的缓存流
                   // CheckedInputStream cos = new CheckedInputStream(bis, new CRC32());//检查读取流，采用CRC32算法，保证文件的一致性
                    byte[] b = new byte[1024];//字节数组，每次读取1024个字节
                    //循环读取压缩文件的值
                    while (bis.read(b) != -1) {
                        bos.write(b);//写入到新文件
                    }
                    bis.close();
                   // cos.close();
                    is.close();
                    bos.close();
                    os.close();
                }
            }
            else
            {
                //如果为空文件夹，则创建该文件夹
                new File(outPath+zipEntry.getName()).mkdirs();
            }
        }
        System.out.println("解压完成");
        zipFile.close();
    }

    public static void main(String[] args) throws Exception {
        deCompress(zipPath,outPath);
    }
}